package thinkinjava4.ch21_concurrency;

import java.util.concurrent.TimeUnit;

import static thinkinjava4.util.Print.print;

class DualSynch {
    private final Object syncObject = new Object();

    public synchronized void f() {
        for (int i = 0; i < 5; i++) {
            print("f()");
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void g() {
        synchronized (syncObject) {
            for (int i = 0; i < 5; i++) {
                print("g()");
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

public class SyncObject {
    public static void main(String[] args) {
        final DualSynch ds = new DualSynch();
//        new Thread() {
//            @Override
//            public void run() {
//                ds.f();
//            }
//        }.start();

//        new Thread(() -> ds.f()).start();
        new Thread(ds::f).start();
        ds.g();
    }
}
